﻿class @ResponsesView extends CustomView

    initialize: ->
        super
        @model.bind("change", @render)
        @model.fetch()

    events: {
        "submit form.general": "addResponse"
        "click ul.comments li h2": "showProfile"
    }

    render: ->
        @GetTemplate(SiteMap.Responses.Template, (compiledTemplate)=>
            $(@el).html(compiledTemplate(@model.toJSON()))
        )

    addResponse: (event) ->
        event.preventDefault()
        form = $("#responses").find("form")
        data = form.serialize()
        Messages.WriteMessage("Добавляется отзыв...")
        $.post(SiteMap.Responses.Save, data, (response) =>
            if response.SuccessMessage? && response.SuccessMessage.length
                Messages.WriteMessage(response.SuccessMessage)
                @model.fetch()
            else
                Messages.WriteErrors(response.Errors)
        )

    showProfile: (e) ->
        e.preventDefault()
        login = $(e.target).html()
        if login? 
            url = SiteMap.Account.ShowProfile + "/" + login
            @Navigate(url , true)
